package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class PrivateCustomerContainer here.
 * 
 * @author (dmab0913 Gruppe4) 
 * @version (a version number or a date)
 */
public class PrivateCustomerContainer
{
    private ArrayList<PrivateCustomer> PrivateCustomers;
    private static PrivateCustomerContainer instance;

    /**
     * Constructor for objects of class PrivateCustomerContainer
     */
    public PrivateCustomerContainer()
    {
        PrivateCustomers = new ArrayList<PrivateCustomer>();
    }

    /**
     * Singleton for the PrivateCustomerContainer class
     */
    public static PrivateCustomerContainer getInstance()
    {
        if (instance == null)
        {
            instance = new PrivateCustomerContainer();
        }
        return instance;
    }

    /**
     * puts a privateCustomer-object into the container
     */
    public void addPrivateCustomers(PrivateCustomer privateCustomer)
    {
        PrivateCustomers.add(privateCustomer);
    }

    /**
     * finds a specific privateCustomers-object from the container
     */
    public PrivateCustomer findPrivateCustomer(String phone)
    {
        int index = 0;
        boolean found = false;
        while(index < PrivateCustomers.size() && !found)
        {
            PrivateCustomer customer = PrivateCustomers.get(index);
            if(phone.equals(customer.getPhone()))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }

        if(found)
        {
            return PrivateCustomers.get(index);
        }
        else
        {
            return null;
        }
    }

    /**
     * removes a privateCustomer-object from the container
     */
    public void deletePrivateCustomer(String phone)
    {
        boolean found = false;
        int index = 0;
        while(index < PrivateCustomers.size() && !found)
        {
            PrivateCustomer l = PrivateCustomers.get(index);
            if(l.getPhone().equals(phone))
            {
                PrivateCustomers.remove(index);
                found = true;
            }
            index++;
        }
    }

    /**
     * finds and updates all the fields from a prvateCustomer-object
     */
    public void updatePrivateCustomer(String phone, String newName, String newAddress, String newPhone, int newPostalCode, String newCity, String newEmail)
    {
        boolean found = false;
        int index = 0;
        while(index < PrivateCustomers.size() && !found)
        {
            PrivateCustomer P = PrivateCustomers.get(index);
            if(P.getPhone().equals(phone))
            {
                P.updatePrivateCustomer(newName, newAddress, newPhone, newPostalCode, newCity, newEmail);
                found = true;
            }
            index++;
        }
    }
}
